package com.sunhao.gulimall.product.web;

import com.sunhao.gulimall.product.entity.CategoryEntity;
import com.sunhao.gulimall.product.service.CategoryService;
import com.sunhao.gulimall.product.vo.Catelog2Vo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class IndexController {
    @Autowired
    CategoryService categoryService;

    /**
     * 跳转首页
     * @param model
     * @return
     */
    @GetMapping({"/","index.html"})
    public String indexPage(Model model)
    {
        //1.查询所有的一级分类
        List<CategoryEntity> entities = categoryService.getLvel1Categorys();
        model.addAttribute("categories",entities);
        log.info(entities.toString());
        return "index";
    }

    //index/json/catalog.json
    @GetMapping(value = "/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson() {

        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;

    }

}
